#import "MO2XMLManagedObjectsImporter.h"


@implementation MO2XMLManagedObjectsImporter

- (id)initWithContentsOfFile:(NSString *)aPath {
	self = [super init];
	if(self) {
		[self setProperties:[NSDictionary dictionary]];
		[self setEntityName:[NSString string]];
		[self setPath:aPath];
		[self setUrl:nil];		
	}
	return self;
}

- (id)initWithContentsOfURL:(NSURL *)aUrl {
	self = [super init];
	if(self) {
		[self setProperties:[NSDictionary dictionary]];
		[self setEntityName:[NSString string]];
		[self setUrl:aUrl];
		[self setPath:nil];
	}
	return self;
}

- (void)dealloc {
	[entityName release];
	[properties release];
	[managedObjectContext release];
	[managedObjectsAsDictionaries release];
	[path release];
	[url release];
	
	[super dealloc];
}

- (BOOL)setupManagedObjectsAsDictionaries {
	if([self path] != nil) {
		[self setManagedObjectsAsDictionaries:[[NSArray arrayWithContentsOfFile:[self path]] retain]];
		return YES;
	}
	if([self url] != nil) {
		[self setManagedObjectsAsDictionaries:[[NSArray arrayWithContentsOfURL:[self url]] retain]];
		return YES;
	}

	return NO;
}

- (NSArray *)managedObjectsAsDictionaries {
	return managedObjectsAsDictionaries;
}

- (void)setManagedObjectsAsDictionaries:(NSArray *)value {
	[value retain];
	[managedObjectsAsDictionaries release];
	managedObjectsAsDictionaries = value;
}

- (NSDictionary *)properties {
	return properties;
}

- (void)setProperties:(NSDictionary *)value {
	[value retain];
	[properties release];
	properties = value;
}

- (BOOL)importManagedObjects {
	NSArray *dictionaries = [self managedObjectsAsDictionaries];
	NSEnumerator *dictionariesEnumerator = [dictionaries objectEnumerator];
	id cardAsDict;
	while(cardAsDict = [dictionariesEnumerator nextObject]) {
		NSArray *cardsKeys = [cardAsDict allKeys];
		
		NSEntityDescription *entityDescription = [NSEntityDescription entityForName:[self entityName] inManagedObjectContext:[self managedObjectContext]];
		
		Class classObject = NSClassFromString([entityDescription managedObjectClassName]);
		NSManagedObject *mo = [[classObject alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:[self managedObjectContext]];
		NSDictionary *propertiesForAll = [self properties];
		NSArray *relationshipsKeys = [propertiesForAll allKeys];
		NSEnumerator *relationshipsKeysEnumerator = [relationshipsKeys objectEnumerator];
		id key;
		while(key = [relationshipsKeysEnumerator nextObject]) {
			id value = [propertiesForAll valueForKey:key];
			[mo setValue:value forKey:key];
		}
		// Iterate through the keys	
		NSEnumerator *cardsKeysEnumerator = [cardsKeys objectEnumerator];
		id cardsKey;
		while(cardsKey = [cardsKeysEnumerator nextObject]) {
			[mo setValue:[cardAsDict valueForKey:cardsKey] forKey:cardsKey];
		}
		[mo release];
	}
	[dictionaries release];
	return YES;
}

- (NSManagedObjectContext *)managedObjectContext {
	if(managedObjectContext == nil) {
		return [[NSApp delegate] managedObjectContext];
	}
	return managedObjectContext;
}

- (void)setManagedObjectContext:(NSManagedObjectContext *)value {
	[value retain];
	[managedObjectContext release];
	managedObjectContext = value;
}

- (NSString *)entityName {
	return entityName;
}

- (void)setEntityName:(NSString *)value {
	[value retain];
	[entityName release];
	entityName = value;
}

- (void)setPath:(NSString *)value {
	[value retain];
	[path release];
	path = value;
	
	[self setupManagedObjectsAsDictionaries];
}

- (NSString *)path {
	return path;
}

- (NSURL *)url {
	return url;
}

- (void)setUrl:(NSURL *)value {
	[value retain];
	[url release];
	url = value;
}

@end
